package codegen.builders;

import java.io.BufferedWriter;
import java.io.IOException;

import org.apache.log4j.Logger;

public class CodegenInterface extends CodegenType {
	transient protected final Logger logger = Logger.getLogger(getClass().getName());

	public CodegenInterface(String simpleName) {
		super(simpleName);
	}

	/**
	 * Note that this does not copy the method passed as a parameter. This is deliberate - usually this method is called by a class from its
	 * {@link CodegenClass#extractInterface()} method. Under these circumstances it is beneficial to point to the same instance of the
	 * method as the calling class, as changes in the method definition for the class will automatically reflect in the interface.
	 */
	public void addMethod(CodegenMethod method) {
		methods.put(method.getName(), method);
		if (method.returns != null) {
			addImport(method.returns);
		}
	}

	@Override
	public void writeMethods(BufferedWriter fw) throws IOException {
		for (CodegenMethod method : methods.values()) {
			writeIndent(fw);
			switch (method.getVisibility()) {
			case PUBLIC:
			case PACKAGE:
				method.writeInterface(fw);
				break;
			default:
				logger.info("Method '" + method.getName() + "' cannot be extracted to an interface with a visibility of " + method.getVisibility()
						.toString());
			}

			newline(fw);
		}
	}

	@Override
	protected void writeClassOpen(BufferedWriter fw) throws IOException {
		doWriteClassOpen(true, fw);
	}

}
